Public Class M_Login
    Dim dtable As New DataTable
    Dim dadapter As New OleDb.OleDbDataAdapter
    Private t0, t1, t2, t3, t4, t5 As String
    Private nw As Boolean
    Private Sub M_Login_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        load_grid()
        detail(DataGridView1.Item(0, DataGridView1.CurrentRow.Index).Value)
        nw = True
    End Sub

    Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        Dim jwb
        nw = False
        If cek_change() Then
            jwb = MsgBox("Data telah berubah, apakah data akan di simpan?", MsgBoxStyle.YesNo, "Save Changes")
            If jwb = 6 Then
                save()
            End If
        End If
        detail(DataGridView1.Item(0, DataGridView1.CurrentRow.Index).Value)
    End Sub

    Private Sub detail(ByVal id)
        open_db("select * from m_login where id=" & id)
        While Read.Read()
            Me.TextBox1.Text = Read(1)
            Me.TextBox2.Text = Read(2)
            Me.TextBox3.Text = isnull(Read(3))
            Me.TextBox4.Text = isnull(Read(4))
            Me.TextBox5.Text = isnull(Read(5))
        End While
        close_db()
        set_param(id)
    End Sub
    Private Sub set_param(ByVal id)
        t0 = id
        t1 = TextBox1.Text
        t2 = TextBox2.Text
        t3 = TextBox3.Text
        t4 = TextBox4.Text
        t5 = TextBox5.Text
    End Sub
    Private Function cek_change() As Boolean
        If TextBox1.Text <> t1 Then
            Return True
        Else
            If TextBox2.Text <> t2 Then
                Return True
            Else
                If TextBox3.Text <> t3 Then
                    Return True
                Else
                    If TextBox4.Text <> t4 Then
                        Return True
                    Else
                        If TextBox5.Text <> t5 Then
                            Return True
                        Else
                            Return False
                        End If
                    End If
                End If
            End If
        End If
    End Function
    Private Function cek_null() As Boolean
        If TextBox1.Text = "" Then
            cek_null = True
        Else
            If TextBox2.Text = "" Then
                cek_null = True
            Else
                If TextBox3.Text = "" Then
                    cek_null = True
                Else
                    If TextBox4.Text = "" Then
                        cek_null = True
                    Else
                        If TextBox5.Text = "" Then
                            cek_null = True
                        Else
                            cek_null = False
                        End If
                    End If
                End If
            End If
        End If
    End Function
    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
        save()
    End Sub
    Private Sub save()
        Dim qry As String
        Dim i As Integer
        If Not cek_null() Then
            If nw Then
                qry = "insert into m_login (User_id,passwd,Nama,Departement,lvl) values ('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox5.Text & "')"
                nw = False
            Else
                qry = "update m_login set "
                i = 0
                If TextBox1.Text <> t1 Then
                    qry = qry + "user_id='" & TextBox1.Text
                    i += 1
                End If
                If TextBox2.Text <> t2 Then
                    If i > 0 Then
                        qry = qry + "',"
                    End If
                    i += 1
                    qry = qry + "passwd='" & TextBox2.Text
                End If
                If TextBox3.Text <> t3 Then
                    If i > 0 Then
                        qry = qry + "',"
                    End If
                    i += 1
                    qry = qry + "nama='" & TextBox3.Text
                End If
                If TextBox4.Text <> t4 Then
                    If i > 0 Then
                        qry = qry + "',"
                    End If
                    i += 1
                    qry = qry + "departement='" & TextBox4.Text
                End If
                If TextBox5.Text <> t5 Then
                    If i > 0 Then
                        qry = qry + "',"
                    End If
                    i += 1
                    qry = qry + "lvl='" & TextBox5.Text
                End If
                qry = qry + "' where id =" & t0
            End If
            exec_qr(qry)
            load_grid()
        Else
            MsgBox("Harap mengisi semua data", MsgBoxStyle.Information, "Isi Data")
        End If
    End Sub
    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        nw = True
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
        TextBox4.Text = ""
        TextBox5.Text = ""
        set_param("")
        TextBox1.Focus()
    End Sub

    Private Sub ToolStripButton7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton7.Click
        DataGridView1.CurrentCell = DataGridView1.Item(1, DataGridView1.RowCount - 1)
        detail(DataGridView1.Item(0, DataGridView1.CurrentRow.Index).Value)
    End Sub

    Private Sub ToolStripButton6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton6.Click
        Dim i As Integer
        If DataGridView1.CurrentRow Is Nothing Then
            i = -1
        Else
            i = DataGridView1.CurrentRow.Index
        End If
        If i + 1 < DataGridView1.RowCount Then
            DataGridView1.CurrentCell = DataGridView1.Item(1, i + 1)
            detail(DataGridView1.Item(0, DataGridView1.CurrentRow.Index).Value)
        End If
    End Sub

    Private Sub ToolStripButton5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton5.Click
        Dim i As Integer
        If DataGridView1.CurrentRow Is Nothing Then
            i = 1
        Else
            i = DataGridView1.CurrentRow.Index
        End If
        If i - 1 >= 0 Then
            DataGridView1.CurrentCell = DataGridView1.Item(1, i - 1)
            detail(DataGridView1.Item(0, DataGridView1.CurrentRow.Index).Value)
        End If
    End Sub

    Private Sub ToolStripButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton4.Click
        DataGridView1.CurrentCell = DataGridView1.Item(1, 0)
        detail(DataGridView1.Item(0, DataGridView1.CurrentRow.Index).Value)
    End Sub
    Private Sub load_grid()
        dtable.Clear()
        open_con()
        dadapter = New OleDb.OleDbDataAdapter("select * from m_login", DbCon)
        dadapter.Fill(dtable)
        DataGridView1.DataSource = dtable
        DataGridView1.Columns(0).Visible = False
        DataGridView1.Columns(2).Visible = False
        'DataGridView1.Columns(4).HeaderText = "Level"
        DataGridView1.RowHeadersVisible = False
        DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        DataGridView1.AutoResizeColumns()
        DataGridView1.ReadOnly = True
        DbCon.Close()
    End Sub

    Private Sub ToolStripButton8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton8.Click
        Me.Close()
    End Sub

    Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click
        Dim x
        Dim sql As String
        x = MsgBox("Apakah yakin akan menghapus data?", MsgBoxStyle.YesNo, "Detele Data")
        If x = 6 Then
            sql = "delete from m_login where id=" & t0
            exec_qr(sql)
            load_grid()
        End If
    End Sub
    Private Sub DataGridView1_Keyup(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown
        If e.KeyCode = Keys.Down Then
            If DataGridView1.CurrentRow.Index + 1 < DataGridView1.RowCount Then
                detail(DataGridView1.Item(0, DataGridView1.CurrentRow.Index + 1).Value)
            End If
        End If
        If e.KeyCode = Keys.Up Then
            If DataGridView1.CurrentRow.Index - 1 >= 0 Then
                detail(DataGridView1.Item(0, DataGridView1.CurrentRow.Index - 1).Value)
            End If
        End If
    End Sub
End Class
